package com.xjx.util;

import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion;
import com.xjx.util.domain.Users;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 工具类方法都是静态的
 */
public class UserUtil {
    static File file =new File ("/Users/edy/myself/new1.txt");


    /**
     * 读取文件中所有的用户
     */
    public static List<Users> getAll() throws IOException {
        List<String> strlist = FileUtils.readLines(
                file, "utf-8");
        List<Users> userList = new ArrayList();
        for (String s : strlist) {
            String[] s1 = s.split(" ");
            Users users = new Users();
            users.setId(s1[0]);
            users.setName(s1[1]);
            users.setAge(s1[2]);
            users.setSex(s1[3]);
            users.setAddress(s1[4]);
            userList.add(users);
        }
        return userList;

    }


    /**
     * 添加一个用户
     */
    public static void addUser(Users users) throws IOException {
        String userStr = "\n"+users.getId()+users.getName()+users.getAge()+users.getSex()+users.getAddress();
        FileUtils.writeStringToFile(file,userStr,"utf-8",true);
    }
    /**
     * 从文件中删除一个用户
     */
    public static void delete(String id) throws IOException {
        List<Users> usersList = getAll();
        FileUtils.writeStringToFile(file,"","utf-8");
        for (Users users :usersList) {
            if (!users.getId().equals(id)){
                FileUtils.writeStringToFile(file,UsertoString(users),
                        "uft-8",true);

            }

        }


    }
    public static String UsertoString(Users users){
        return "\n"+users.getId()+users.getName()+users.getAge()+users.getSex()+users.getAddress();
    }
    /**
     * 将string转换成Users对象
     */
    public static Users StrtoUsers(String str){
        String[] s1 = str.split(" ");
        Users users = new Users();
        users.setId(s1[0]);
        users.setName(s1[1]);
        users.setAge(s1[2]);
        users.setSex(s1[3]);
        users.setAddress(s1[4]);

        return users;
    }
    /**
     * 根据id号，查询一个用户
     */
    public static Users fineOne(Integer id) throws IOException {
        List<Users> usersList = getAll();
        for (Users users :usersList) {
            if (users.getId().equals(usersList)){
                return users;
            }
        }
        return null;
    }
    /**
     * 修改指定用户的信息
     */
    public static  void update(Users users) throws IOException {
        List<Users> usersList = getAll();
        FileUtils.writeStringToFile(file,"","UTF-8");

        for(Users user1 : usersList)
        {
            //如果user的id和传入的id一样，这一行就不用写入，否则就写入到文档
            if(users.getId().equals(user1.getId()))
            {
                FileUtils.writeStringToFile(file,UsertoString(users),"UTF-8",true);
            }
            else
            {
                FileUtils.writeStringToFile(file,UsertoString(user1),"UTF-8",true);
            }
        }
    }
    public static void main(String[] args) throws IOException {
        //System.out.println(getAll());
        Users users = new Users("5","杨幂","24","女","北京");
        delete("1");
    }
}
